<?php

# get input variables
# format - json or RSS
#error_reporting(E_ALL & E_NOTICE);

# Includes
require_once("xml_regex.inc.php");
require_once("../mn_ini.php");

// re-order the files
$format = $_REQUEST['format'];



$handle = opendir($folder);

# Making an array containing the files in the current directory:
while ($file = readdir($handle)){   
	if( end(explode(".", $file)) == "xml"){
        $key = date("c", filemtime($folder . $file));
        $files[$file] = $key ;
    }
}
closedir($handle);
// sort files by mtime:


arsort($files) ;

# Function to get Title tag from XML file
function getMetaTitle($metafile) {
	global $folder;
	$xml = file_get_contents($folder . $metafile);
	$title = value_in('title', $xml);
	return $title;
}

# Function to get Title tag from XML file
function getMetaAbstract($metafile) {
	global $folder;
	$xml = file_get_contents($folder . $metafile);
	$abstract = value_in('abstract', $xml);
	return $abstract;
}


#echo the files


if ($format == "html") {
	$i = 0;
	foreach ($files as $key => $value) {
		if ($i < 5) {
			# This is the top 5. Need nice date, title from metadata, and 
			$thelink = "loadMeta('" . $key . "');";
			//echo '<a href="javascript:void(0)" onclick="' . $thelink . '">' . getMetaTitle($key) . ' - ' . $value . '</a>'.'<br />' ;
			echo '<a href="javascript:void(0)" onclick="' . $thelink . '">' . getMetaTitle($key) . '</a>'.'<br />' ;
		}
		$i++;
	}
}
elseif ($format == "rss") {
	$i = 0;
	include("feedcreator.class.php");
	$rss = new UniversalFeedCreator();
	$rss->useCached();
	$rss->title = "Metadata Navigator";
	$rss->description = "Latest metadata changes for Mecklenburg County GIS";
	$rss->link = "http://maps.co.mecklenburg.nc.us/metadata";
	$rss->syndicationURL = "http://maps.co.mecklenburg.nc.us/metdata/services/ws_metadata_latest.php?format=rss";
	foreach ($files as $key => $value) {
		if ($i < 10) { 
			$item = new FeedItem();
			$item->title = getMetaTitle($key);
			$item->link = "http://maps.co.mecklenburg.nc.us/metadata/default.htm?search=" . $key;
			$item->description = getMetaAbstract($key);
			$item->date = $value;
			$rss->addItem($item);
		}
		$i++;
	}
	$xml=$rss->createFeed("2.0");
	echo $xml;
}

?>